﻿using System;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System.Data.SqlClient;

namespace SMALLERP.SY
{
    public partial class FormPassWord : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr;
        public FormPassWord()
        {
            InitializeComponent();
           

        }
        
        //输入用户文本框敲回车
        private void txtUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter )
            {
                txtOldPassWord.Focus();
            }
        }

        //输入原密码文本框敲回车
        private void txtOldPassWord_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter )
            {
                txtPassWord.Focus();
            }
        }
        ////输入新密码文本框敲回车
        private void txtPassWord_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter )
            {
                txtAgainPassWord.Focus();
            }
        }
        //重复密码文本框敲回车键
        private void txtAgainPassWord_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOk_Click(sender, e);
            }
        }



        private void btnOk_Click(object sender, EventArgs e)
        {
            string strSql = null;

            if (String.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show("用户名不许为空！", "软件提示");
                txtUserName.Focus();
                return;
            }
 
            //密码
            string Passwordjm = txtOldPassWord.Text.Trim();
            //用户名
            string UserName = txtUserName.Text.Trim();


            //strSql = "SELECT Passwordjm=dbo.EncodePwd('" + PropertyClass.OperatorCode + "','" + txtOldPassWord.Text.Trim() + "') FROM DSCMA";


            //MessageBox.Show(strSql);
            //MessageBox.Show(PropertyClass.PassWord);
            //密码校验
            strSql = "SELECT LOGONNAME,USER_NAME,UDF024,UDF025,UDF026,USER_ID FROM [USER] WHERE LOGONNAME='" + txtUserName.Text + "' AND UDF026='" + txtOldPassWord.Text + "'";
            try
            {
                sdr = db.GetDataReader(strSql);
                sdr.Read();
                if (sdr.HasRows)
                {
                    //AppMain POForm = new AppMain();
                    //AppPO POForm = new AppPO();
                    //Hide();
                    PropertyClass.ID = sdr["USER_ID"].ToString();
                    PropertyClass.OperatorCode = sdr["LOGONNAME"].ToString();
                    PropertyClass.OperatorName = sdr["USER_NAME"].ToString();
                    PropertyClass.PassWord = sdr["UDF026"].ToString();
                    //PropertyClass.IsAdmin = sdr["MA005"].ToString();
                    //PropertyClass.WeiXinAccount = sdr["MA006"].ToString();
                    //PropertyClass.jiaose = sdr["UDF024"].ToString();
                    //PropertyClass.gzzx = sdr["UDF025"].ToString();
                    //POForm.Show();
                }
                else
                {
                    MessageBox.Show("用户名或用户密码不正确！", "软件提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("用户名或用户密码不正确！", "软件提示");
                return;
            }
            finally
            {
                sdr.Close();
               
            }


            try
            {
            //sdr = db.GetDataReader(strSql);    
            //sdr.Read();
            //Passwordjm = sdr["Passwordjm"].ToString(); 

            //MessageBox.Show(" PropertyClass.OperatorCode: " + PropertyClass.OperatorCode + ",Passwordjm: "+ Passwordjm+ ",PropertyClass.PassWord: "+ PropertyClass.PassWord);
            //return;
            //if(UserName != PropertyClass.OperatorCode)
            //{
            //        MessageBox.Show("用户名不正确！", "软件提示");
            //        txtUserName.Focus();
            //        return;
            //}

            if (Passwordjm != PropertyClass.PassWord)
            {
                MessageBox.Show("原密码不正确！", "软件提示");
                txtOldPassWord.Focus();
                return;
            }
            }
           catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
            finally
            {
                //sdr.Close();
            }

            if (String.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show("用户名不许为空！", "软件提示");
                txtPassWord.Focus();
                return;
            }


            if (String.IsNullOrEmpty(txtPassWord.Text))
            {
                MessageBox.Show("新密码不许为空！", "软件提示");
                txtPassWord.Focus();
                return;
            }

            if (String.IsNullOrEmpty(txtAgainPassWord.Text))
            {
                MessageBox.Show("确认密码不许为空！", "软件提示");
                txtAgainPassWord.Focus();
                return;
            }

            if (txtPassWord.Text != txtAgainPassWord.Text)
            {
                MessageBox.Show("两次输入的密码不相同！", "软件提示");
                txtPassWord.Focus();
                return;
            }

            //strSql = "Update DSCMA Set MA003 = dbo.EncodePwd('" + PropertyClass.OperatorCode + "','" + txtPassWord.Text.Trim() + "') Where MA001 = '" +
            //         PropertyClass.OperatorCode + "'";
            strSql = "Update [USER] Set UDF026 = '"  + txtPassWord.Text.Trim() + "' Where LOGONNAME = '" +
                     PropertyClass.OperatorCode + "'";

            try
            {
                if (db.ExecDataBySql(strSql) > 0)
                {
                    MessageBox.Show("密码修改成功！", "软体提示");
                }
                else
                {
                    MessageBox.Show("密码修改失败！", "软体提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软体提示");
                throw ex;
            }

            Close();
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormPassWord_Load(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void txtOldPassWord_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {

        }
    }
}